﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JZNJ.RadioMonitorServer.Core
{
    public class DrawPicture
    {
        DeviceManager device = new DeviceManager();
        public Graphics gra;

        //绘制坐标系
        public void DrawCoordinate(Graphics e,int width,int height,Rectangle rec)
        {
            gra = e;
            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, width, height), Color.Black, Color.Black, 90, false);
            gra.FillRectangle(brush, rec);
            Pen p = Pens.Gray;

            int x = 80;
            int y = 80;

            for (int i = 0; i < 27; i++)
            {
                gra.DrawLine(p, x, 80, x, 620);
                x = x + 30;
            }

            for (int i = 0; i < 19; i++)
            {
                gra.DrawLine(p, 80, y, 860, y);
                y = y + 30;
            }

            //坐标原点为80,440,后边需要进行转化及自定义
            //绘制X轴与Y轴及其箭头
            Font font = new Font("宋体", 8);
            PointF m_pOrigin = new PointF(80, 620);
            PointF m_pYMax = new PointF(80, 60);
            PointF m_PYLeft = new PointF(m_pYMax.X - 6, m_pYMax.Y + 10);
            PointF m_PYRight = new PointF(m_pYMax.X + 6, m_pYMax.Y + 10);
            gra.DrawLines(Pens.White, new PointF[] { m_PYLeft, m_pYMax, m_PYRight });//画Y轴的箭头
            gra.DrawString("功率", font, Brushes.White, m_pYMax.X - 20, m_pYMax.Y - 10);

            PointF m_pXMax = new PointF(880, 620);
            PointF m_pXUp = new PointF(m_pXMax.X - 10, m_pXMax.Y - 6);
            PointF m_pXDown = new PointF(m_pXMax.X - 10, m_pXMax.Y + 6);
            gra.DrawLines(Pens.White, new PointF[] { m_pXUp, m_pXMax, m_pXDown });//画X轴的箭头
            gra.DrawString("频率", font, Brushes.White, m_pXMax.X + 20, m_pXMax.Y - 6);

            gra.DrawLines(Pens.White, new PointF[] { m_pYMax, m_pOrigin, m_pXMax });//画X轴及Y轴

            //绘制X轴的刻度
            for (int i = 0; i < 27; i++)
            {
                string l_str = (1.59 + 0.04 * i).ToString();
                gra.DrawString(l_str, font, Brushes.White, float.Parse((m_pOrigin.X + i * 30 - 20).ToString()), Convert.ToSingle(m_pOrigin.Y));
            }

            //获得功率的最大值和最小值,用于绘制Y轴的刻度
            float l_dMaxValue = 100;
            float l_dMinValue = 70;

            double l_dSetp = (l_dMaxValue - l_dMinValue) / 20;
            //绘制Y轴的刻度
            for (int i = 18; i >= 0; i--)
            {
                string l_str = (l_dMaxValue - l_dSetp * i).ToString("0.00");
                gra.DrawString(l_str, new Font("宋体", 8), Brushes.White, Convert.ToSingle(80) - 30, float.Parse((80 + i * 30 - 15).ToString()));
            }
        }

        //绘制折线
        public void DrawBrokenLine(Graphics e)
        {
            Graphics gra = e;
            PointF p_one;
            PointF p_last = new PointF();
            int j = 0;
            for (double start = 1.59E+9; start < 2.6E+9; start += 0.003E+9)
            {
                device.GetFrequency(start);
                if (start == 1.59E+9)
                {
                    p_one = new PointF(80, Convert.ToSingle(device.Frequency[0].Trim()) * -3);
                    p_last = p_one;
                }
                else
                {
                    p_one = new PointF(float.Parse((80 + j * 2.31).ToString()),
                        Convert.ToSingle(device.Frequency[0].Trim()) * -10 - 500);
                }

                gra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                gra.DrawLine(Pens.Aqua, p_last, p_one);
                p_last = p_one;
                j++;
            }
        }
    }
}
